package com.cec.home.controller

import com.cec.home.service.impl.BgImageServiceImpl
import com.cec.home.service.impl.MainNavServiceImpl
import com.cec.home.service.impl.NewsServiceImpl
import com.cec.home.service.impl.PeopleServiceImpl
import com.cec.home.utils.PKUtils
import com.mongodb.BasicDBObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
/**
 * Created with IDEA
 * User:hxh
 * Date:16/4/22
 * Time:上午11:06
 */
@Controller
@RequestMapping("index")
class IndexController {

    @Autowired
    MainNavServiceImpl mainNavService

    @Autowired
    BgImageServiceImpl bgImageService

    @Autowired
    PeopleServiceImpl peopleService

    @Autowired

    NewsServiceImpl newsService

    @RequestMapping(value = "", method = RequestMethod.GET)
    def index(Model model) {
        //导航条

        def navs = mainNavService.getAll([status: 1] as BasicDBObject, [sort: 1] as BasicDBObject, null)

        model.addAttribute("navs", navs)

        //新闻展示

        def leftNews = newsService.getAll([status: [$gt: 0] as BasicDBObject] as BasicDBObject, [status: 1] as BasicDBObject, null, 3)

        def rightNews = newsService.getAll([status: [$gte: 0] as BasicDBObject] as BasicDBObject, [created: -1] as BasicDBObject, null, 3)

        model.addAttribute("leftNews", leftNews)

        model.addAttribute("rightNews", rightNews)

        //首页图片

        def list = bgImageService.getAll(null, [img: 1, type: 1] as BasicDBObject, null)

        def imgs = new HashMap()

        list.each {
            it.img = PKUtils.buildImg(it.img as String)
            imgs.put(it.type,it.img)
        }

        model.addAttribute("imgs", imgs)


        return "index"
    }
}
